package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.enums.EnableStatusEnum;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 机构认证审核表 服务实现类
 * </p>
 *
 * @author wei
 * @since 2025-01-18
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {

    @Autowired
    private AgencyCertificationAuditMapper agencyCertificationAuditMapper;
    @Autowired
    private AgencyCertificationMapper agencyCertificationMapper;

    @Autowired
    private ServeProviderMapper serveProviderMapper;

    @Override
    public void certificationAudit (AgencyCertificationAuditAddReqDTO reqDTO) {
        Long l = UserContext.currentUserId ();
        if (l == null) {
            throw new RuntimeException ("用户未登录");
        }

        reqDTO.setServeProviderId (l);

        // TODO
        AgencyCertificationAudit agencyCertificationAudit = BeanUtils.copyBean (reqDTO, AgencyCertificationAudit.class);
        AgencyCertification certification = BeanUtils.copyBean (reqDTO, AgencyCertification.class);
        certification.setId (l);
        certification.setCertificationStatus (CertificationStatusEnum.PROGRESSING.getStatus ());

        AgencyCertificationAudit agencyCertification = agencyCertificationAuditMapper.selectOne (Wrappers.<AgencyCertificationAudit>lambdaQuery ()
                .eq (AgencyCertificationAudit::getServeProviderId, l));
        if (agencyCertification != null) {
            if (agencyCertification.getCertificationStatus () == CertificationStatusEnum.FAIL.getStatus ()) {
                int i = agencyCertificationMapper.updateById (certification);
                if (i <= 0) {
                    throw new RuntimeException ("认证信息提交失败");
                }
                agencyCertificationAudit.setId (agencyCertification.getId ());
                agencyCertificationAudit.setAuditStatus (EnableStatusEnum.DISABLE.getStatus ());
                agencyCertificationAudit.setCertificationStatus (CertificationStatusEnum.PROGRESSING.getStatus ());
                this.updateById (agencyCertificationAudit);
            }
        } else {
            int insert = agencyCertificationMapper.insert (certification);
            if (insert <= 0) {
                throw new RuntimeException ("认证信息提交失败");
            }

            boolean save = this.save (agencyCertificationAudit);
            if (!save) {
                throw new RuntimeException ("认证审核提交失败");
            }
        }
    }

    @Override
    public PageResult<AgencyCertificationAuditResDTO> page (AgencyCertificationAuditPageQueryReqDTO dto) {
        Page<AgencyCertificationAudit> agencyCertificationAuditPage = agencyCertificationAuditMapper.selectPage (
                new Page<> (dto.getPageNo (), dto.getPageSize ()),
                Wrappers.<AgencyCertificationAudit>lambdaQuery ()
                        .eq (dto.getAuditStatus () != null, AgencyCertificationAudit::getAuditStatus, dto.getAuditStatus ())
                        .eq (dto.getCertificationStatus () != null, AgencyCertificationAudit::getCertificationStatus, dto.getCertificationStatus ())
                        .like (dto.getName () != null, AgencyCertificationAudit::getName, dto.getName ())
                        .like (dto.getLegalPersonName () != null, AgencyCertificationAudit::getLegalPersonName, dto.getLegalPersonName ())
                        .orderBy (dto.getOrderBy1 () != null, dto.getIsAsc1 (), AgencyCertificationAudit::getCreateTime)
        );
        return PageUtils.toPage (agencyCertificationAuditPage, AgencyCertificationAuditResDTO.class);
    }

    @Override
    public void audit (Long id, String rejectReason, Integer certificationStatus) {
        AgencyCertificationAudit agencyCertificationAudit = agencyCertificationAuditMapper.selectById (id);
        if (agencyCertificationAudit == null) {
            throw new RuntimeException ("审核信息不存在");
        }
        Long serveProviderId = agencyCertificationAudit.getServeProviderId ();
        AgencyCertification agencycertification = agencyCertificationMapper.selectById (serveProviderId);
        if (agencycertification == null) {
            throw new RuntimeException ("认证信息不存在");
        }
        agencycertification.setCertificationStatus (certificationStatus);
        agencycertification.setCertificationTime (LocalDateTime.now ());
        int i1 = agencyCertificationMapper.updateById (agencycertification);
        if (i1 <= 0) {
            throw new RuntimeException ("审核失败");
        }
        agencyCertificationAudit.setCertificationStatus (certificationStatus);
        agencyCertificationAudit.setRejectReason (rejectReason);
        agencyCertificationAudit.setAuditStatus (EnableStatusEnum.ENABLE.getStatus ());
        agencyCertificationAudit.setAuditorId (UserContext.currentUserId ());
        agencyCertificationAudit.setAuditorName (UserContext.currentUser ().getName ());
        agencyCertificationAudit.setAuditTime (LocalDateTime.now ());
        ServeProvider serveProvider = serveProviderMapper.selectById (serveProviderId);
        if (serveProvider == null) {
            throw new RuntimeException ("服务商不存在");
        }
        serveProvider.setName (agencyCertificationAudit.getName ());
        serveProviderMapper.updateById (serveProvider);
        int i = agencyCertificationAuditMapper.updateById (agencyCertificationAudit);

        if (i <= 0) {
            throw new RuntimeException ("审核失败");
        }
    }

    @Override
    public String rejectReason () {
        return this.getById (UserContext.currentUserId ()).getRejectReason ();
    }
}
